<?php

namespace App\Models\Order;

use App\Models\Custom\Custom;
use App\Models\Invoice\Invoice;
use Illuminate\Database\Eloquent\Model;

class Outboundorder extends Model
{
    protected $table = 'outbound_order';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     * 列表
     */
    public static function getList($request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $utype = $memberinfo['utype'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;

        $query = self::with(['suborder' => function ($query) {
            $query->where('return_status', '!=', config('constants.return_goods_status.pass'));

            //开票日期
            //$start_invoice_time = $request->start_invoice_time;
            //if ($start_invoice_time != '') {
            //    $query->where('invoice_time', '>=', $start_invoice_time);
            //}
            //
            //$end_invoice_time = $request->end_invoice_time;
            //if ($end_invoice_time != '') {
            //    $query->where('invoice_time', '<=', $end_invoice_time);
            //}

        }])->with('custom')->with('invoice')->orderBy('create_at', 'desc');

//        $query = $query->where("is_create_contract","1");

        $invoice_status = $request->invoice_status;
        if ($invoice_status > 0) {
            $query->whereHas('suborder', function ($query) use ($invoice_status) {
                $query->where('invoice_status', $invoice_status);
            });
        }

        $examine_status = $request->examine_status;
        if ($examine_status > 0) {
            $query->whereHas('suborder', function ($query) use ($examine_status) {
                $query->where('examine_status', $examine_status);
            });
        }

        $settlement_company = $request->settlement_company;
        if ($settlement_company != '') {
            $query->where('settlement_company', 'like', '%' . $settlement_company . '%');
        }

        //单据日期
        $start_time = $request->start_time;
        if ($start_time != '') {
            $query->where('document_date', '>=', $start_time);
        }

        $end_time = $request->end_time;
        if ($end_time != '') {
            $query->where('document_date', '<=', $end_time);
        }

        //客户名称
        $custom_name = $request->custom_name;
        if ($custom_name != '') {
            $query->where('custom_name', 'like', '%' . $custom_name . '%');
        }

        //单据编号
        $document_number = $request->document_number;
        if ($document_number != '') {
            $query->where('document_number', 'like', '%' . $document_number . '%');
        }

        $order_number = $request->order_number;
        if ($order_number != '') {
            $query->where('order_number', 'like', '%' . $order_number . '%');
        }

        /**
         *  manger 1自己添加的数据，2所有数据
         *  销售只能看到自己的出库单数据,其他人数据不隔离 2025.03.27 王丹娜说的
         *
        */
        $manger = $request->manger;
        if ($manger == 1 || $utype == '7') {
            $query->where('manger_id', '=', $manger_id);
        }

        if ($cpid > 0) {
            $query->where('cpid', $cpid);
        }

        // if ($sys_dpm_id > 0) {
        //     $query->where('sys_dpm_id', $sys_dpm_id);
        // }

        //开票状态
        $invoice_status = $request->invoice_status;
        if ($invoice_status == 2) {
            $query->where('invoice_status', '=', $invoice_status);
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    /**
     * 详情
     */
    public static function getone($id)
    {
        return self::with(['suborder','invoice'])->where('id', $id)->first();
    }

    /**
     * 获取子订单
     */
    public function suborder()
    {
        return $this->hasMany(Outboundordersub::class, 'p_id', 'id');
    }

    /**
     * 客户信息
     */
    public function custom()
    {
        return $this->hasOne(Custom::class, 'id', 'custom_id');
    }

    /**
     * 开票信息
     */
    public function invoice()
    {
        return $this->hasOne(Saleorderinvoice::class, 'order_number', 'order_number');
    }
}
